package com.das.mechanic_base.mvp.view.camera;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.app.X3AppRouterConstants;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.main.CarBrandAndCommonBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = X3AppRouterConstants.Router.QUICK_CAMERA)
public class X3QuickCameraActivity extends X3BaseActivity {

    @BindView(R2.id.include_title)
    RelativeLayout include_title;
    @BindView(R2.id.iv_icon_one)
    ImageView iv_icon_one;
    @BindView(R2.id.iv_icon_two)
    ImageView iv_icon_two;
    @BindView(R2.id.iv_icon_us)
    ImageView iv_icon_us;
    @BindView(R2.id.iv_icon_one2)
    ImageView iv_icon_one2;
    @BindView(R2.id.iv_icon_us2)
    ImageView iv_icon_us2;
    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.tv_set)
    TextView tv_set;

    private String mobile;
    private long workBaseId = 0L;

    private boolean photoFirst = true;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_activity_quick_camera;
    }

    @Override
    protected void initView() {
        X3StatusBarUtil.darkMode(this);
        include_title.setPadding(0, X3StatusBarUtil.getStatusBarHeight(this),0,0);
        include_title.setBackgroundColor(Color.WHITE);
        tv_title.setText(getString(R.string.switch_camera_mode));

//        workBaseId = getIntent().getLongExtra("workBaseId", 0L);

        int type = getIntent().getIntExtra("type", 0);

        tv_set.setVisibility(workBaseId == 0 ? View.GONE : View.VISIBLE);

        tv_set.setText(type == 0 ? getString(R.string.switch_camera_mode_alone_base) : getString(R.string.switch_camera_mode_work_base));

        mobile = (String) SpHelper.getData("mobile", "");
        int cameraMode;
        if (workBaseId == 0) {
            cameraMode = (int) SpHelper.getData(mobile + X3Constent.QUICK_CAMERA_MODE, 2);

        }else {
            cameraMode = (int) SpHelper.getData(mobile + X3Constent.QUICK_CAMERA_MODE + workBaseId, -1);
            if (cameraMode == -1) {
                cameraMode = (int) SpHelper.getData(mobile + X3Constent.QUICK_CAMERA_MODE, 2);
            }
        }
        resetImage();
        resetCameraOrder();
        switch (cameraMode) {
            case 0:
                iv_icon_one.setImageResource(R.mipmap.x3_car_select);
                break;
            case 1:
                iv_icon_us.setImageResource(R.mipmap.x3_car_select);
                break;
            case 2:
                iv_icon_two.setImageResource(R.mipmap.x3_car_select);
                break;
        }

        getCheckQuestionSet();

    }

    private void resetImage() {
        iv_icon_one.setImageResource(R.mipmap.x3_car_no_select);
        iv_icon_two.setImageResource(R.mipmap.x3_car_no_select);
        iv_icon_us.setImageResource(R.mipmap.x3_car_no_select);
    }

    private void resetCameraOrder() {
        iv_icon_one2.setImageResource(R.mipmap.x3_car_no_select);
        iv_icon_us2.setImageResource(R.mipmap.x3_car_no_select);
    }

    @Override
    protected X3BasePresenter createPresenter() {
        return null;
    }

    @OnClick({R2.id.rl_high_most, R2.id.rl_high,R2.id.rl_middle,R2.id.iv_back,R2.id.rl_high_most2,R2.id.rl_high2})
    public void onViewClick(View view) {
        int id = view.getId();
        if (id == R.id.rl_high_most) {
            resetImage();
            iv_icon_one.setImageResource(R.mipmap.x3_car_select);
            SpHelper.saveData(mobile + X3Constent.QUICK_CAMERA_MODE + (workBaseId == 0 ? "" : workBaseId), 0);
            finish();
        } else if (id == R.id.rl_high) {
            resetImage();
            iv_icon_us.setImageResource(R.mipmap.x3_car_select);
            SpHelper.saveData(mobile + X3Constent.QUICK_CAMERA_MODE + (workBaseId == 0 ? "" : workBaseId), 1);
            finish();
        } else if (id == R.id.rl_middle) {
            resetImage();
            iv_icon_two.setImageResource(R.mipmap.x3_car_select);
            SpHelper.saveData(mobile + X3Constent.QUICK_CAMERA_MODE + (workBaseId == 0 ? "" : workBaseId), 2);
            finish();
        } else if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.rl_high_most2) {
            resetCameraOrder();
            iv_icon_one2.setImageResource(R.mipmap.x3_car_select);
            photoFirst = true;
            updateCheckQuestionSet(true);
        } else if (id == R.id.rl_high2) {
            resetCameraOrder();
            iv_icon_us2.setImageResource(R.mipmap.x3_car_select);
            photoFirst = false;
            updateCheckQuestionSet(false);
        }
    }

    //获取拍照顺序
    private void getCheckQuestionSet(){
        NetWorkHttp.getApi().getCheckQuestionSet()
                .compose(this.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object carBrandBeans) {

                        boolean cameraModel = (boolean) carBrandBeans;
                        SpHelper.saveData(mobile + X3Constent.ALONE_CAMERA_ORDER, cameraModel);
                        photoFirst = cameraModel;
                        if (cameraModel){
                            iv_icon_one2.setImageResource(R.mipmap.x3_car_select);
                        } else {
                            iv_icon_us2.setImageResource(R.mipmap.x3_car_select);
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    //获取拍照顺序
    private void updateCheckQuestionSet(boolean first){
        showLoading("");
        NetWorkHttp.getApi().updateCheckQuestionSet(first)
                .compose(this.bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object carBrandBeans) {
                        X3QuickCameraActivity.this.hideLoading();
                        SpHelper.saveData(mobile + X3Constent.ALONE_CAMERA_ORDER, photoFirst);
                        finish();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {
                        X3QuickCameraActivity.this.hideLoading();
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("设置拍摄模式列表页");
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("设置拍摄模式列表页");
    }
}